from django.contrib.auth.models import User
from rest_framework.serializers import ModelSerializer,SerializerMethodField
from school.models import Classes
from user.serializers import UserSerializer



class ClassesSerializer(ModelSerializer):
    # member = UserSerializer(many=True)
    member_detail = SerializerMethodField()
    class Meta:
        model= Classes
        exclude = ['is_delete']
        extra_kwargs = {
            'member':{'write_only':True}
        }


    def get_member_detail(self, classes):
        serializer = UserSerializer(classes.member.all(),many=True)
        data = {
            'teacher':[i for i in serializer.data if 1 in i['groups'] or i['is_superuser']],
            'student':[i for i in serializer.data if 2 in i['groups']]
        }
        return data


